/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package it.dileo.gui;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Mario
 */
public class TestFrame extends javax.swing.JFrame {

    Persona p_da_salvare = null;
    List<String> nomiMaterie = new ArrayList<String>();
    /**
     * Creates new form TestFrame
     */
    public TestFrame() {
        initComponents();
        System.out.println("costruttore");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField_Nome = new javax.swing.JTextField();
        jTextField_Cognome = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jCheckBox_studente = new javax.swing.JCheckBox();
        jLabel_materia = new javax.swing.JLabel();
        jTextField_materia = new javax.swing.JTextField();
        jButton_addMateria = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Nome:");

        jLabel2.setText("Cognome:");

        jTextField_Cognome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField_CognomeActionPerformed(evt);
            }
        });

        jButton1.setText("Salva");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jCheckBox_studente.setText("studente");
        jCheckBox_studente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_studenteActionPerformed(evt);
            }
        });

        jLabel_materia.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel_materia.setText("materia");
        jLabel_materia.setEnabled(false);

        jTextField_materia.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jTextField_materia.setForeground(new java.awt.Color(0, 0, 204));
        jTextField_materia.setEnabled(false);

        jButton_addMateria.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/dileo/gui/icons/add2_32.png"))); // NOI18N
        jButton_addMateria.setEnabled(false);
        jButton_addMateria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_addMateriaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField_Cognome)
                            .addComponent(jTextField_Nome)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCheckBox_studente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 107, Short.MAX_VALUE)
                        .addComponent(jLabel_materia, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField_materia, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton_addMateria, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jSeparator1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jTextField_Cognome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox_studente)
                    .addComponent(jLabel_materia)
                    .addComponent(jTextField_materia, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_addMateria))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(4, 4, 4))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField_CognomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField_CognomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField_CognomeActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(jCheckBox_studente.isSelected()){
            p_da_salvare = new Studente();
        }else{
            p_da_salvare = new Persona();
        }
        
       
        String nome = jTextField_Nome.getText();
        String cognome = jTextField_Cognome.getText();
        
        p_da_salvare.setNome(nome);
        p_da_salvare.setCognome(cognome);
        
        if(p_da_salvare instanceof Studente){
            for (String nomeMateria : nomiMaterie) {
                ((Studente)p_da_salvare).studia(new Materia(nomeMateria));
            }
        }
        
        p_da_salvare.salva();
        
        if(p_da_salvare instanceof Studente){
            List<Materia> materie = ((Studente)p_da_salvare).getMaterie();
            for (Materia materia : materie) {
                System.out.println(p_da_salvare.getNome() + " sta studiando: "+materia.getNome());
            }
        }
        
        jTextField_Cognome.setText("");
        jTextField_Nome.setText("");
        p_da_salvare = null;
        nomiMaterie.clear();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jCheckBox_studenteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox_studenteActionPerformed
        boolean selected = jCheckBox_studente.isSelected();
        
        jLabel_materia.setEnabled(selected);
        jTextField_materia.setEnabled(selected);
        jButton_addMateria.setEnabled(selected);
        
        
        
    }//GEN-LAST:event_jCheckBox_studenteActionPerformed

    private void jButton_addMateriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_addMateriaActionPerformed

            String nomeMateria = jTextField_materia.getText();
            nomiMaterie.add(nomeMateria);
            jTextField_materia.setText("");
            
            
        
    }//GEN-LAST:event_jButton_addMateriaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton_addMateria;
    private javax.swing.JCheckBox jCheckBox_studente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel_materia;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField_Cognome;
    private javax.swing.JTextField jTextField_Nome;
    private javax.swing.JTextField jTextField_materia;
    // End of variables declaration//GEN-END:variables
}
